<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Nav;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Nav::orderBy('nav_order','asc')->get();
        return view('admin.nav.index',['data'=>$data]);
    }

    //改变分类排序
    public function changeOrder()
    {
        $input = Input::except('_token');
//        dd($input);
        $f = Nav::find($input['nav_id']);
        $f->nav_order = $input['nav_order'];
        $re = $f->save();
        if($re){
            $data =[
                'status'=>0,
                'msg'=>'排序成功'
            ];
        }else{
            $data =[
                'status'=>1,
                'msg'=>'排序失败'
            ];
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.nav.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $input = Input::except('_token');
        $rule=[
            'nav_name'=>'required',
            'nav_url'=>'required',
        ];

        $mess=[
            'nav_name.required'=>'导航名称必须输入',
            'nav_url.required'=>'导航链接必须输入',
        ];

        $validator =  Validator::make($input,$rule,$mess);
        if($validator->passes()){

            $re = Nav::create($input);
            if($re){
                return redirect('admin/nav');
            }else{
                return back()->with('errors','导航项添加失败');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data =  Nav::where('nav_id',$id)->first();
        //可以使用compact函数给模板传变量
        return view('admin.nav.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = Input::except(['_token','_method']);
        $re = Nav::where('nav_id',$id)->update($input);

        if($re){
            return redirect('admin/nav');
        }else{
            return back()->with('errors','链接修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $re =   Nav::where('nav_id',$id)->delete();
        if($re){
            $data = [
                'status'=>0,
                'msg'=>'友情链接删除成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'友情链接删除失败'
            ];
        }
        return $data;
    }
}
